/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(honeysoft@126.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.scores.serviceimpl;

import java.io.InputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.teasoft.bee.osql.Condition;
import org.teasoft.bee.osql.Op;
import org.teasoft.bee.osql.OrderType;
import org.teasoft.bee.osql.Suid;
import org.teasoft.bee.osql.SuidRich;
//import org.teasoft.beex.poi.ExcelExceptionTest;
//import org.teasoft.beex.poi.ExcelReader;
import org.teasoft.honey.osql.core.BeeFactoryHelper;
import org.teasoft.honey.osql.core.ConditionImpl;
import org.teasoft.honey.util.SuidHelper;

import com.aiteasoft.school.admin.entity.Assigncourse;
import com.aiteasoft.school.scores.entity.Scores;
import com.aiteasoft.school.scores.service.ScoresForTeacherService;
import com.automvc.common.jq.PageWarp;
import com.automvc.common.jq.Result;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-01-18 18:29:21
 */
@Service
public class ScoresForTeacherServiceImpl implements ScoresForTeacherService {

	@Autowired
	Suid suid;
	
	@Autowired
	SuidRich suidRich;

	@Override
	public Scores process(Scores entity) { 
		//Add the business logic if need. 放一些公共设置
		//判断用户角色. TODO   要是没有权限直接抛业务异常,导到无权限页面.
		entity.setTeacherno("60002");//TODO 从登录信息获取
		
		return entity;
	}
	
	@Override
	public boolean isWrongBizForUpdate(Result result, Scores entity) {
		//Add the business logic if need.
		if("FIN".equals(entity.getStatus())) {
			result.setErrorMsg("The recored don't allow to update!");
			return true;
		}
		return false;
	}
	
	@Override
	public PageWarp countAndSelect(Scores entity, int start, int size) {
	    Condition condition = getCondition(entity);
	    process(entity);//只能查询自己名下的数据
		int total = suidRich.count(entity, condition);
		
		condition.start(start);
		condition.size(size);
		condition.orderBy("term", OrderType.DESC).orderBy("classno");
		List<Scores> listPage=suidRich.select(entity, condition);
		
		return new PageWarp(total,listPage);
	}
	
	private Condition getCondition(Scores entity) {
//		Condition condition=new ConditionImpl();
//		Condition condition=BeeFactory.getHoneyFactory().getCondition();
		Condition condition=BeeFactoryHelper.getCondition();
		//Add the business logic if need.
//		condition.op("classno", Op.in, getClassnoList(entity)); //closed 2021-4
		return condition;
	}
	
    private String getClassnoList(Scores entity) {
		Assigncourse assigncourse=new Assigncourse();
		assigncourse.setStatus("1");  //查当前有效的数据
		assigncourse.setTeacherno(entity.getTeacherno());
		List<String[]> list=suidRich.selectString(assigncourse, "classno");
		String classnoList=SuidHelper.parseFirstColumn(list);
		
		return classnoList;
    }

	@Override
	public int update(Scores entity) {
		//Add the business logic if need.

		Scores scores=new Scores();
		scores.setId(entity.getId());
		scores.setScore(entity.getScore());
		
		scores.setRemark(entity.getRemark());
		
		process(scores);//只能改自己名下的数据
		scores.setUpdater("老师账号");//TODO
		scores.setStatus("UPDATE");
		
		Condition condition=new ConditionImpl();
		condition.op("status", Op.nq, "FIN"); //状态为还没关闭的,才能修改
		
		//前端页面判断,状态为FIN的不能修改. TODO
		
//		return suid.updateById(scores,condition);  
		
		return suidRich.update(scores,"score,remark,updater,status",condition); 
		
	}

	@Override
	public int uploadScore(InputStream inputStream) throws Exception {
		/*System.err.println(ExcelExceptionTest.getString());
		String checkTitles = "班级编号,学期,考试编号,课程号,课程名称,学号,学生姓名,老师编号,分数";
		//		List<String[]> listStringArray = ExcelReader.checkAndReadExcel(inputStream, checkTitles, 0);
		List<String[]> listStringArray = ExcelReader.checkAndReadExcel(inputStream, checkTitles, 0);
		String fields = "classno,term,examno,subjectno,subject,stuno,name,teacherno,score";
		List<Scores> listScore = SuidHelper.parseToEntity(listStringArray, 1, fields, new Scores());
		int total = 0;
		Scores scores;
		suidRich.beginSameConnection();
		for (int i = 0; listScore != null && i < listScore.size(); i++) {
			scores=listScore.get(i);
			scores.setStatus("UPLOAD");
			//TODO 判断是否与登录的老师账号一样,如果不一样,直接Exception
			
		//			TODO
			Condition condition=BeeFactoryHelper.getCondition();
			condition.op("status", Op.nq, "FIN"); //状态为还没关闭的,才能修改
			
			total += suidRich.update(scores, "score,status",condition);
		}
		suidRich.endSameConnection();
		return total;*/
		return 0;
	}

}